function close_iframe(){
	//$("#print_iframe").attr("src","").hide();
}
function iframe_print(){
	$("#print_iframe").attr("src","/order/order/print/code/QA0200203160001");
}

function print(){
    var tn=$("#tracking_number").val();
	if(tn.length>0){
	    //window.location.href='http://www.sfcservice.com/order/print/index/orderCodeList/'+tn;
	   try{
	      $("#print_order").attr("src",'');
		  var url='/order/order/print/code/'+tn;
		  $("#print_order").attr("src",url);
		  //setTimeout(function(){
		      //alert(url);
		      window.frames['print_order'].focus();
              window.frames['print_order'].print();

		  //},1000);
		
       }catch(e){
          alert("<{$lang.print}>脚本<{$lang.exception}>："+e.description);
       }

	}else{
	   alert("<{$lang.can_not_find}><{$lang.print}><{$lang.data}>!");
	}
	
}
//以上js 无作用 -------------------------------------------------------------------------------------------

var products = {};
var type="product";
var not_check_label = 0;
//直接<{$lang.print}> 
function printPage(){
 document.getElementById("WebBrowser").ExecWB(6,1);
 }

// <{$lang.print}>设置
 function printSetup(){
  document.getElementById("WebBrowser").ExecWB(8,1);
 }

// <{$lang.print}><{$lang.view}>
 function printPreview(){
  document.getElementById("WebBrowser").ExecWB(7,1);
 }
 
 var isIE = 0;
 if (+[1,]){//非IE
 	isIE = 0;	
 }else {//IE
 	isIE = 1;
 	
 }

 var timeout = 0;


function printFp()
{		
	if(isIE){
		alertTip("请使用firefox，该<{$lang.function}>不支持ie");
		return false;
	}
	var allok = true;
	$("#order_products tr").each(function(){
		var quantity = $.trim($(this).children(".quantity").text());
		var quantity1 = $.trim($(this).children("td:last").text());
		if(quantity!=quantity1){
			allok = false;
		}
	});
//		alert(allok);
	if(allok||not_check_label==1){
		loading();
		clearTimeout(timeout);
		var tn=$("#tracking_number").val();
		if(tn==''){return;}
		$.ajax({
	        type: "POST",
	        async: false,
	        dataType: "html",
	        url: "/order/order/print-pop/code/"+tn+"/ordersCode/"+$("#orders_code").val(),
	        success: function(html) {				
				var  pwin=window.open("a.html","print",'height=200, width=200, top=-200, left=-200, toolbar=no, menubar=no, scrollbars=yes,resizable=yes');   
	         	pwin.document.write(html);   
	          	pwin.document.close();             //这句很重要哦  
	         	
	         	if(isIE){
		         	pwin.document.all.WebBrowser.ExecWB(6,2);
	         	}else{
	         		pwin.print();	         		
	         	}
	         	
	    		
	         	timeout = setTimeout(function(){		         		
//	         		$("#orders_code_ch").val("");
		         	$("#product_sku").val("");
		         	//$("#order_products").html("");
		         	$("#pp_name").html("");
		         	$("#orders_code_ch").focus();	
	         	},2000);
	         		         	
	         	closeLoading();
	         	$("#print_result").append("<div><a href='http://admin.chinafullfill.com/order/order/print-pop/code/"+tn+"/ordersCode/"+$("#orders_code").val()+"/js/no' target='_blank'><{$lang.orders_code}>："+$("#orders_code").val()+"<{$lang.shipping_code}>："+tn+"<{$lang.print}></a></div>");

	        }
		});
	}         
}	
function myfocus(){
	$("#orders_code_ch").focus();	
}
window.onload = myfocus;
$(function () {
	
    $("#orders_code_ch_btn").click(function () {    	
    	
    	var orders_code_ch = $.trim($("#orders_code_ch").val());    	
        if(orders_code_ch==""){
        	type = "order";
        	alertTip("<{$lang.pls_input}><{$lang.orders_code}>");
        	return false;
        }
        $("#order_products").html("");
        loading();
        $.ajax({
            type: "post",
            async: false,
            dataType: "json",
            url: "/warehouse/print-logistics/order-product/orders_code/"+orders_code_ch,
           
            success: function(json) {
            	products = {};
            	$("#orders_code_ch").select();
            	if(json.ask==1){       		
            		var html = "";
            		$.each(json.products,function(k,v){
            			html+="<tr id='"+v.customer_id+"-"+v.product_sku+"'>";
            			products[v.customer_id+"-"+v.product_sku] = v.customer_id+"-"+v.product_sku;
            			html+="<td ondblclick='quantity_add(this,"+v.op_id+");'>"+v.customer_id+"-"+v.product_sku+"</td>";
            			html+="<td title="+v.product_id+">"+v.title_cn+"</td>";
            			html+="<td>"+v.title+"</td>";
            			html+="<td>"+"<img src='"+v.pic+"' height='75'>"+"</td>";
            			html+="<td class='quantity'>"+v.op_quantity+"</td>";
            			html+="<td  ondblclick='quantity_del(this);'>0"+"</td>";            			
            			
            			html+="</tr>";
            			
            		})
            		$("#order_products").html(html);
            		$("#tracking_number").val(json.tracking_number);
            		$("#orders_code").val(orders_code_ch);
            		$("#pp_name").text(json.pp_name);
            		not_check_label = json.not_check_label;
            		
            		if(not_check_label==1){//直接<{$lang.print}>
            			printFp();
            			return;
            		}
                	$("#product_sku").focus();     
            	}else{
                	$("#orders_code_ch").focus();
                	type = "order";
            		alertTip(json.msg);
            	}
            	closeLoading();
            	
                	
            	
            }
        });
    }).focus(function(){
    	$("#orders_code_ch").select();
     });

    $("#product_sku_btn").click(function(){
	    	
    		type="product";
			var <{$lang.sku}> = $.trim($("#product_sku").val());
			$("#product_sku").select();
			if(<{$lang.sku}>==""||!products[<{$lang.sku}>]){
				alertTip("<{$lang.orders_code}>不包含该<{$lang.sku}><{$lang.or}>未<{$lang.input}><{$lang.sku}>");
				return false;
			}
			var td = $("#"+<{$lang.sku}>).children("td:last");
			var quantity = parseInt(td.text());
			quantity++;

			var td1 = $("#"+<{$lang.sku}>).children(".quantity");
			var quantity1 = parseInt(td1.text());
			if(quantity>quantity1){
				alertTip("<{$lang.quantity}>已经最大");
				return false;
			}
			td.text(quantity);
			printFp();
        });
  //回车处理
	$("#orders_code_ch").keyup(function(e){
		var key = e.which;
		if(key==13){
			$("#orders_code_ch_btn").click();
			$("#orders_code_ch").blur();
		}
	});
	 //回车处理
	$("#product_sku").keyup(function(e){
		var key = e.which;
		if(key==13){
			$("#product_sku_btn").click();
		}
	});
	
});

//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>(<{$lang.close}><{$lang.esc}>)"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,        
        close:function(){
        	if(type=="order"){
            	$("#orders_code_ch").focus();        		
        	}else{
            	$("#product_sku").focus();
        		
        	}
            }
    });
}

//闪烁
function bling(obj){
	$(obj).parent().css("color","red");
	setTimeout(function(){
		$(obj).parent().css("color","#333");
		},500);
}
function quantity_add(obj,op_id){
	$("#product_sku").focus();      
	var quantity = $(obj).parent().children("td:last").text();
	quantity = parseInt(quantity);
	quantity++;
	if(quantity>parseInt($(obj).siblings(".quantity").text())){
		alertTip("<{$lang.quantity}>已经最大");
		return false;
	}
	$(obj).parent().children("td:last").text(quantity);
	bling(obj);
	printFp();
}

function quantity_del(obj){
	$("#product_sku").focus();      
	var quantity = $(obj).text();
	quantity = parseInt(quantity);
	quantity--;
	if(quantity<0){
		alertTip("<{$lang.quantity}>已经最小");
		return false;
	}
	$(obj).text(quantity);
	bling(obj);
}